package com.framework.network.interceptor

import android.content.Context
import com.framework.network.annotations.getCommonParams
import com.framework.network.annotations.getCommonParamsParameter
import com.framework.network.annotations.getRetrofitMethod
import com.framework.network.interceptor.commoninter.ParamsContext
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @Description:设置公共参数拦截器
 * @Author: you
 */
class CommonParamsInterceptor(private val context: Context) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        val retrofitMethod = getRetrofitMethod(request) ?: return chain.proceed(request)
        val commonParams = getCommonParams(retrofitMethod)
        val isAddCommonParam = if (commonParams != null) {
            getCommonParamsParameter(commonParams)
        } else {
            true
        }
        if (isAddCommonParam) {
            request = ParamsContext(request, context).inRequest
        }
        return chain.proceed(request)
    }
}